﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FirmaTransportowa
{
    public partial class AdminAddNewCar : Form
    {

        public AdminAddNewCar()
        {
            SqlConnection dbConnection2 = SqlConnectionFactory.getConnection();
            InitializeComponent();
        }

        Vehicle editedVehicle;
        bool editMode = false;
        public event Action eventClosed;

        public AdminAddNewCar(Vehicle vehicle, bool edit)
        {
            editedVehicle = vehicle;
            editMode = edit;
            InitializeComponent();
        }

        private void addNewCarButton_Click(object sender, EventArgs e)
        {
            bool fieldsFilledOut = true;

            try
            {
                long test2 = Convert.ToInt64(capacityBox.Text.Trim().ToString());
                long test3 = Convert.ToInt64(sweptVolumeBox.Text.Trim().ToString());
            }
            catch (FormatException ex)
            {
                fieldsFilledOut = false;
                MessageBox.Show("Pojemność i pojemność skokowa muszą składać się z liczb!"); 
            }

            try
            {
                string spoko = licenseIDBox.SelectedItem.ToString();
            }
            catch (NullReferenceException ex)
            {
                fieldsFilledOut = false;
                MessageBox.Show("Wybierz wymagane prawo jazdy");
            }

            if (carTypeBox.Text.Trim() == String.Empty)
            {
                carTypeError.Text = "Podaj rodzaj samochodu";
                carTypeError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if (capacityBox.Text.Trim() == String.Empty)
            {
                capacityError.Text = "Podaj pojemność samochodu";
                capacityError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if (engineNumberBox.Text.Trim() == String.Empty)
            {
                engineNumberError.Text = "Podaj numer silnika";
                engineNumberError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if (sweptVolumeBox.Text.Trim() == String.Empty)
            {
                sweptVolumeError.Text = "Podaj pojemność skokową";
                sweptVolumeError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if (brandBox.Text.Trim() == String.Empty)
            {
                brandError.Text = "Podaj markę samochodu";
                brandError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if (modelBox.Text.Trim() == String.Empty)
            {
                modelError.Text = "Podaj model samochodu";
                modelError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if (editMode == false)
            {
                if ((fieldsFilledOut) && (!Vehicle.checkIfRecordExists(engineNumberBox.Text.Trim().ToString(), "Vehicle")))
                {

                    Vehicle vehicle = Vehicle.getByEngineNumber(engineNumberBox.Text.Trim().ToString());
                    if (vehicle == null)
                    {
                        vehicle = new Vehicle();
                        vehicle.active = true;
                    }

                    vehicle.car_type = carTypeBox.Text.Trim().ToString();
                    vehicle.capacity = int.Parse(capacityBox.Text.Trim().ToString());
                    vehicle.engine_number = engineNumberBox.Text.Trim().ToString();
                    vehicle.swept_volume = int.Parse(sweptVolumeBox.Text.Trim().ToString());
                    vehicle.brand = brandBox.Text.Trim().ToString();
                    vehicle.model = modelBox.Text.Trim().ToString();
                    vehicle.active = true;

                    string spoko = licenseIDBox.SelectedItem.ToString();
                    int licenceID = 1;
                    switch (spoko)
                    {
                        case "B":
                            licenceID = 1;
                            break;
                        case "B+E":
                            licenceID = 2;
                            break;
                        case "C":
                            licenceID = 3;
                            break;
                        case "C+E":
                            licenceID = 4;
                            break;
                        case "C1":
                            licenceID = 5;
                            break;
                        case "D":
                            licenceID = 6;
                            break;
                        case "D+E":
                            licenceID = 7;
                            break;
                        case "D1":
                            licenceID = 8;
                            break;
                    }
                    vehicle.licence_id = licenceID;
                    vehicle.save();
                    MessageBox.Show("Dodano samochód");
                    this.Dispose();

                    if (eventClosed != null)
                    {
                        eventClosed();
                    }
                }
            }

            if (editMode == true)
                if (fieldsFilledOut)
                {
                    {
                        editedVehicle.car_type = carTypeBox.Text.Trim().ToString();
                        editedVehicle.capacity = int.Parse(capacityBox.Text.Trim().ToString());
                        editedVehicle.engine_number = engineNumberBox.Text.Trim().ToString();
                        editedVehicle.swept_volume = int.Parse(sweptVolumeBox.Text.Trim().ToString());
                        editedVehicle.brand = brandBox.Text.Trim().ToString();
                        editedVehicle.model = modelBox.Text.Trim().ToString();
                        editedVehicle.active = true;

                        string spoko = licenseIDBox.SelectedItem.ToString();
                        int licenceID = 1;
                        switch (spoko)
                        {
                            case "B":
                                licenceID = 1;
                                break;
                            case "B+E":
                                licenceID = 2;
                                break;
                            case "C":
                                licenceID = 3;
                                break;
                            case "C+E":
                                licenceID = 4;
                                break;
                            case "C1":
                                licenceID = 5;
                                break;
                            case "D":
                                licenceID = 6;
                                break;
                            case "D+E":
                                licenceID = 7;
                                break;
                            case "D1":
                                licenceID = 8;
                                break;
                        }
                        editedVehicle.licence_id = licenceID;
                        editedVehicle.save();
                        MessageBox.Show("Zmieniono samochód");

                        this.Dispose();

                        if (eventClosed != null)
                        {
                            eventClosed();
                        }
                    }
                }
        }

        private void AdminAddNewCar_Load_1(object sender, EventArgs e)
        {
            carTypeBox.Text = editedVehicle.car_type.ToString();
            capacityBox.Text = editedVehicle.capacity.ToString();
            engineNumberBox.Text = editedVehicle.engine_number.ToString();
            sweptVolumeBox.Text = editedVehicle.swept_volume.ToString();
            brandBox.Text = editedVehicle.brand.ToString();
            modelBox.Text = editedVehicle.model.ToString();
            int licence = editedVehicle.licence_id;
            switch (licence)
            {
                case 1:
                    licenseIDBox.SelectedIndex = 0;
                    break;
                case 2:
                    licenseIDBox.SelectedIndex = 1;
                    break;
                case 3:
                    licenseIDBox.SelectedIndex = 2;
                    break;
                case 4:
                    licenseIDBox.SelectedIndex = 3;
                    break;
                case 5:
                    licenseIDBox.SelectedIndex = 4;
                    break;
                case 6:
                    licenseIDBox.SelectedIndex = 5;
                    break;
                case 7:
                    licenseIDBox.SelectedIndex = 6;
                    break;
                case 8:
                    licenseIDBox.SelectedIndex = 7;
                    break;
            }
        }

        private void AdminAddNewCar_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            HelpUtils.showHelp(this, 26);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F1)
            {
                if(editMode==false)
                    HelpUtils.showHelp(this, 26);
                else
                    HelpUtils.showHelp(this, 27);
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
